package com.hrb.douban.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hrb.douban.CustomApplication;
import com.hrb.douban.R;
import com.hrb.douban.record.PersonRecord;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class CollectPageAdapter extends RecyclerView.Adapter<CollectPageAdapter.CollectPageHolder> {

    private ArrayList list;

    public CollectPageAdapter(ArrayList list) {
        this.list = list;
    }

    @NonNull
    @Override
    public CollectPageHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.collect_person_item_layout, viewGroup, false);
        CollectPageHolder vholder = new CollectPageHolder(view);
        return vholder;
    }

    @Override
    public void onBindViewHolder(@NonNull CollectPageHolder collectPageHolder, int i) {
        if(PersonRecord.class.equals(list.get(i).getClass())){
            PersonRecord personRecord=(PersonRecord) list.get(i);
            Glide.with(CustomApplication.getContext()).load(personRecord.getAvatars().getMedium()).centerCrop().into(collectPageHolder.personAvatar);
            collectPageHolder.personBorn.setText(personRecord.getBorn_place());
            collectPageHolder.personNameCh.setText(personRecord.getName());
            collectPageHolder.personNameEn.setText(personRecord.getName_en());
            collectPageHolder.timeItem.setText(personRecord.getAddTime());
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class CollectPageHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.time_item)
        TextView timeItem;
        @BindView(R.id.person_avatar)
        CircleImageView personAvatar;
        @BindView(R.id.person_nameCh)
        TextView personNameCh;
        @BindView(R.id.person_name_en)
        TextView personNameEn;
        @BindView(R.id.person_born)
        TextView personBorn;
        @BindView(R.id.person_work)
        TextView personWork;
        public CollectPageHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public ArrayList<PersonRecord> getList() {
        return list;
    }

    public void setList(ArrayList<PersonRecord> list) {
        this.list = list;
    }
}
